﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace KeyboardCount
{
    public partial class Form1 : Form
    {
        public void Synchronous()
        {
            try
            {
                SendAndRecrive senderRcv = new SendAndRecrive();
                if (!senderRcv.SendFile(tcpClient, SendAndRecrive.State.UPLOAD))//如果上传时发生异常，例如服务器挂了
                {
                    Invoke(new CheckLoginOutDelegate(CheckLoginOut), new object[] { " " });
                }                
            }
            catch (System.Exception ex)
            {
                Invoke(new CheckLoginOutDelegate(CheckLoginOut), new object[] { " " });
            }
            
            Thread.CurrentThread.Abort();
        }

        protected delegate void CheckLoginOutDelegate(string rcvMsg);

        public void CheckLoginOut(string str)
        {
            timerCheckLoginOut.Start();
            labelUser.Text = "与服务器中断,正在进行自动连接";
            IsLogin = false;
            tcpClient.Close();
        }

        private void timerCheckLoginOut_Tick(object sender, EventArgs e)
        {
            try
            {
                SendAndRecrive sendRcv = new SendAndRecrive();
                tcpClient = new TcpClient(sendRcv.IP, sendRcv.SRV_PORT);
                sendRcv.SendUsernameAndPasswd(tcpClient, username, passwd, SendAndRecrive.State.LOGIN);
                if (sendRcv.errno == 0)
                {
                    timerCheckLoginOut.Stop();
                    labelUser.Text = "Hello," + username;
                    IsLogin = true;
                }
                else
                {
                    IsLogin = false;
                    tcpClient.Close();
                }
            }
            catch (System.Exception ex)
            {
                
            }
        }

        public void CheckAutoLogin()
        {
            try
            {
                UserInfo user = new UserInfo();
                if (File.Exists("Info.dat"))
                {
                    FileStream file = new FileStream("Info.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
                    BinaryFormatter b = new BinaryFormatter();
                    user = b.Deserialize(file) as UserInfo;
                    file.Close();
                }
                if (user.IsRemember && user.IsAutoLogin)
                {
                    SendAndRecrive sendRcv = new SendAndRecrive();
                    TcpClient tcpclient = new TcpClient(sendRcv.IP, sendRcv.SRV_PORT);
                    sendRcv.SendUsernameAndPasswd(tcpclient, user.username, user.password, SendAndRecrive.State.LOGIN);
                    if (sendRcv.errno == 0)
                    {
                        username = user.username;
                        passwd = user.password;
                        this.tcpClient = tcpclient;
                        labelUser.Text = "Hello," + user.username;
                        linkLabelLogin.Text = "注销";
                        IsLogin = true;
                        CheckNew();
                    }
                    else
                    {
                        if (sendRcv.errno == 1)
                            MessageBox.Show("用户名不存在！");
                        else if (sendRcv.errno == 2)
                            MessageBox.Show("密码错误！");
                        else if (sendRcv.errno == 3)
                            MessageBox.Show("该用户已登录！");

                        IsLogin = false;
                        tcpclient.Close();
                    }  
                }
            }
            catch (System.Exception e)
            {
            	
            }
        }
    }
}
